<?php 

/**
* 数据操作类
*/
class Model 
{
	public $table;//表名
	public $errorInfo; //错误信息
	public static $db; //pdo链接对象
	public $where; //where 条件
	/**
	 * [__construct 构造函数]
	 * @Author   Xuebingsi
	 * @DateTime 2017-09-25T09:57:19+0800
	 * @param    string                   $table [description]
	 */
	public function __construct($table ='')
	{
		// 赋值表名
		$this->table = $table;

		try {
			//判断是否有链接过数据库
			if(!self::$db){
				self::$db = new PDO(config('DB_TYPE').":host=".config('DB_HOST').";dbname=".config('DB_NAME').";charset=utf8",config('DB_USERNAME'),config('DB_PASSWORD'));
			}

		} catch (PDOException $e) {
			$this->errorInfo="Error: ".$e;
		}
	}
	/**
	 * [getError 获取错误信息]
	 * @Author   Xuebingsi
	 * @DateTime 2017-09-25T09:58:08+0800
	 * @return   [type]                   [description]
	 */
	public function getError()
	{
		return $this->errorInfo;
	}
	/**
	 * [add 增加数据库]
	 * @Author   Xuebingsi
	 * @DateTime 2017-09-25T09:58:22+0800
	 * @param    [type]                   $data [description]
	 */
	public function add($data)
	{

		$keys = array_keys($data);
		$values = implode("','", $data);
		$name = implode(',', $keys);
		
		$sql = "insert into ".$this->table." (".$name.") values('".$values."')";

		return $this->exec($sql);

	}
	/**
	 * [del 获取主键]
	 * @Author   Xuebingsi
	 * @DateTime 2017-09-25T08:40:58+0800
	 * @param    [type]                   $id [description]
	 * @return   [type]                       [description]
	 */
	public function del($id='')
	{

		if($id){

			$primaryKey = $this->getPrimaryKey();

			$sql = "delete from ".$this->table." where ".$primaryKey."=".$id;

			$this->exec($sql);

		}else{

			$sql = "delete from ".$this->table.$this->where;

			$this->exec($sql);
		}
		

	}
	/**
	 * [query 执行查询]
	 * @Author   Xuebingsi
	 * @DateTime 2017-09-25T09:58:34+0800
	 * @param    [type]                   $sql [需要查询的语句]
	 * @return   [type]                        [description]
	 */
	public function query($sql)
	{
		$res = self::$db->query($sql);

		if($res){
			return $res->fetchAll(PDO::FETCH_ASSOC);
		}else{

			 $this->errorInfo= "错误代码".self::$db->errorCode().':'.self::$db->errorInfo()[2];
		}
	}
	/**
	 * [exec 执行增删改]
	 * @Author   Xuebingsi
	 * @DateTime 2017-09-25T09:58:58+0800
	 * @param    [type]                   $sql [description]
	 * @return   [type]                        [description]
	 */
	public function exec($sql)
	{

		$res = self::$db->exec($sql);

		if($res){
			return true;
		}else{
			$this->errorInfo= "错误代码".self::$db->errorCode().':'.self::$db->errorInfo()[2];
		}
	}
	/**
	 * [getPrimaryKey 获取主键]
	 * @Author   Xuebingsi
	 * @DateTime 2017-09-25T08:40:47+0800
	 * @return   [type]                   [description]
	 */
	public function getPrimaryKey()
	{
		$sql = "desc ".$this->table;

		$data = $this->query($sql);

		$key = '';

		foreach ($data as $row) {
			if($row['Key']=='PRI'){
				$key = $row['Field'];
			}
		}

		return $key;
	}
	/**
	 * [where 增加where条件]
	 * @Author   Xuebingsi
	 * @DateTime 2017-09-25T09:59:12+0800
	 * @param    [type]                   $where [description]
	 * @return   [type]                          [description]
	 */
	public function where($where)
	{

		$sql = " where ";
		foreach ($where as $k => $v) {
			$sql.= $k."='".$v."' and ";
		}

		$this->where = rtrim($sql,'and ');

		return $this;
	}
	/**
	 * [save 更新方法]
	 * @Author   Xuebingsi
	 * @DateTime 2017-09-25T09:59:32+0800
	 * @param    [type]                   $data [description]
	 * @return   [type]                         [description]
	 */
	public function save($data)
	{

		$str = '';

		foreach ($data as $k => $v) {
			$str .= $k."='".$v."',";
		}

		$str = rtrim($str,',');

		$sql ="update ".$this->table." set ".$str." ".$this->where;

		return $this->exec($sql);
	}
	/**
	 * [findOne 查询一条数据]
	 * @Author   Xuebingsi
	 * @DateTime 2017-09-25T09:59:42+0800
	 * @param    string                   $value [description]
	 * @return   [type]                          [description]
	 */
	public function findOne($value='')
	{
		$sql="select * from ".$this->table.$this->where." limit 1";

		return $this->query($sql);
	}
	/**
	 * [findAll 查询全部数据]
	 * @Author   Xuebingsi
	 * @DateTime 2017-09-25T09:59:54+0800
	 * @param    string                   $value [description]
	 * @return   [type]                          [description]
	 */
	public function findAll($value='')
	{
		$sql="select * from ".$this->table.$this->where;

		return $this->query($sql);
	}
}














 ?>